package com.sc.common.entity;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class ThreadUtil {
    private static final Logger LOG = LoggerFactory.getLogger(ThreadUtil.class);
    public static final String USER_ID = "ui";
    public static final String USER_SHOW_NAME = "usn";
    public static final String SIGN_NAME = "sn";
    public static final String QQ = "qq";
    public static final String USER_TYPE = "tp";
    public static final String USER_TYPE_LOGON_PROJECT = "tplp";
    public static final String REAL_NAME = "rn";
    public static final String NICK = "nc";
    public static final String USER_ICON = "uic";
    public static final String SECTION = "sc";
    public static final String GRADE_YEAR = "gy";
    public static final String STYPE = "st";
    public static final String SCHOOL_ID = "sid";
    public static final String CLASS_ID = "cid";
    public static final String SCHOOL_NAME = "snm";
    public static final String CLASS_NAME = "cnm";
    public static final String ACL_CODE = "ac";
    public static final String TEACHER_ID = "tid";
    public static final String LOGIN_IP = "lip";
    public static final String EQUIP_TYPE = "et";
    public static final String TOKEN = "tk";
    public static final String LOGIN_TIME = "lt";
    private static final String TRACE_MSG_ID = "tmi";
    private static final String SRC_PROJECT_NAME = "spn";
    private static final String CLIENT_PROJECT_NAME = "cpn";
    private static final String SRC_IP = "si";
    private static final String CLIENT_IP = "ci";
    private static final String IS_APP_REQ = "iar";
    private static ThreadLocal<Map<String, Object>> threadInfoMap = new ThreadLocal();

    public ThreadUtil() {
    }

    public static void put(String key, Object val) {
        Map<String, Object> infoMap = (Map)threadInfoMap.get();
        if (infoMap == null) {
            Class var3 = ThreadUtil.class;
            synchronized(ThreadUtil.class) {
                infoMap = (Map)threadInfoMap.get();
                if (infoMap == null) {
                    infoMap = new HashMap();
                    threadInfoMap.set(infoMap);
                }
            }
        }

        ((Map)infoMap).put(key, val);
    }

    public static void putIfAbsent(String key, Object val) {
        if (get(key) == null) {
            put(key, val);
        }

    }

    public static Map<String, Object> getThreadInfoMap() {
        return (Map)(threadInfoMap.get() == null ? new HashMap() : Collections.unmodifiableMap((Map)threadInfoMap.get()));
    }

    public static void putAllThreadInfo(Map<String, Object> infoMap) {
        if (infoMap != null && !infoMap.isEmpty()) {
            Map<String, Object> currMap = (Map)threadInfoMap.get();
            if (currMap == null) {
                Class var2 = ThreadUtil.class;
                synchronized(ThreadUtil.class) {
                    currMap = (Map)threadInfoMap.get();
                    if (currMap == null) {
                        currMap = new HashMap();
                        threadInfoMap.set(currMap);
                    }
                }
            }

            ((Map)currMap).putAll(infoMap);
        }

    }

    public static <T> T get(String key) {
        Map<String, T> infoMap = (Map<String,T>)threadInfoMap.get();
        return infoMap == null ? null : infoMap.get(key);
    }

    public static void setToken(String token) {
        putIfAbsent("tk", token);
    }

    public static String getToken() {
        return (String)get("tk");
    }

    public static void setAppReqFlag() {
        putIfAbsent("iar", true);
    }

    public static boolean isAppReq() {
        Object val = get("iar");
        return val != null && (Boolean)val;
    }

    public static void setUserInfo(long userId, String userName, int userType) {
        putIfAbsent("ui", userId);
        putIfAbsent("usn", userName);
        putIfAbsent("tp", userType);
    }

    public static Long getUserId() {
        return (Long)get("ui");
    }

    public static String getUserName() {
        return (String)get("usn");
    }

    public static Integer getUserType() {
        return (Integer)get("tp");
    }

    public static String getTraceMsgId() {
        return (String)get("tmi");
    }

    public static void setSrcProject(String projectName) {
        putIfAbsent("spn", projectName);
    }

    public static void setClientProject(String projectName) {
        put("cpn", projectName);
    }

    public static String getSrcProject() {
        return (String)get("spn");
    }

    public static String getClientProject() {
        return (String)get("cpn");
    }

    public static void setSrcIP(String ip) {
        putIfAbsent("si", ip);
    }

    public static void setClientIP(String ip) {
        put("ci", ip);
    }

    public static String getSrcIP() {
        return (String)get("si");
    }

    public static String getClientIP() {
        return (String)get("ci");
    }

    public static void setTraceMsgId(String traceMsgId) {
        if (StringUtils.isNotBlank(traceMsgId)) {
            putIfAbsent("tmi", traceMsgId);
        } else {
            LOG.warn("traceMsgId has already set, and will ignore this operation.");
        }

    }

    public static void genTraceMsgId() {
        String traceId = System.nanoTime() + (long)new Random().nextInt(100000) + "";
        setTraceMsgId(traceId);
    }

    public static void clear() {
        threadInfoMap.remove();
    }
}
